from django.contrib import admin

from algorithm.models import TagDependency
from notification.models import Notification
from .models import TagChangeRequest, TagDependencyChangeRequest
from django.contrib import admin
from django.utils import timezone
from django.core.mail import send_mail
from .models import TagChangeRequest


@admin.register(TagChangeRequest)
class TagChangeRequestAdmin(admin.ModelAdmin):
    list_display = ('user', 'problem', 'tag', 'add_or_delete', 'is_approved', 'created_at', 'reviewed_at', 'reviewer')
    list_filter = ('is_approved', 'add_or_delete', 'created_at', 'problem', 'is_approved')
    search_fields = ('user__username', 'problem__problem_name', 'tag__tag_name')

    fields = ('user', 'problem', 'tag', 'add_or_delete', 'is_approved')

    def save_model(self, request, obj, form, change):
        if not obj.user_id:
            obj.user = request.user

        if obj.is_approved is not None and not obj.reviewer_id:
            obj.reviewer = request.user
            obj.reviewed_at = timezone.now()
            action = "添加" if obj.add_or_delete == 1 else "删除"

            if obj.is_approved:
                if obj.add_or_delete == 1:
                    obj.problem.add_tag(obj.tag)
                elif obj.add_or_delete == 0:
                    obj.problem.remove_tag(obj.tag)

                # 审核通过通知
                Notification.create_notification(
                    sender="系统通知",
                    user_id=obj.user.id,
                    message=f'您的关于题目 "{obj.problem.problem_name}" 的标签 "{obj.tag.tag_name}" 的 "{action}" 请求已通过审核。'
                )
            else:
                # 审核拒绝通知
                Notification.create_notification(
                    sender="系统通知",
                    user_id=obj.user.id,
                    message=f'您的关于题目 "{obj.problem.problem_name}" 的标签 "{obj.tag.tag_name}" 的 "{action}" 请求未通过审核。'
                )

        super().save_model(request, obj, form, change)


@admin.register(TagDependencyChangeRequest)
class TagDependencyChangeRequestAdmin(admin.ModelAdmin):
    list_display = ('user', 'source_tag', 'target_tag', 'weight', 'add_or_delete', 'is_approved', 'created_at', 'reviewed_at', 'reviewer')
    list_filter = ('is_approved', 'add_or_delete', 'created_at', 'source_tag', 'target_tag')
    search_fields = ('user__username', 'source_tag__tag_name', 'target_tag__tag_name')

    fields = ('user', 'source_tag', 'target_tag', 'weight', 'add_or_delete', 'is_approved')

    def save_model(self, request, obj, form, change):
        if not obj.user_id:
            obj.user = request.user

        if obj.is_approved is not None and not obj.reviewer_id:
            obj.reviewer = request.user
            obj.reviewed_at = timezone.now()
            action = "添加" if obj.add_or_delete == 1 else "删除"

            if obj.is_approved:
                if obj.add_or_delete == 1:
                    TagDependency.create_or_update(obj.source_tag.index, obj.target_tag.index, obj.weight)
                else:
                    TagDependency.objects.filter(source_tag=obj.source_tag, target_tag=obj.target_tag).delete()

                Notification.create_notification(
                    sender="系统通知",
                    user_id=obj.user.id,
                    message=f'您的关于依赖 "{obj.source_tag.tag_name} -> {obj.target_tag.tag_name}" 的 "{action}" 请求已通过审核。'
                )
            else:
                Notification.create_notification(
                    sender="系统通知",
                    user_id=obj.user.id,
                    message=f'您的关于依赖 "{obj.source_tag.tag_name} -> {obj.target_tag.tag_name}" 的 "{action}" 请求未通过审核。'
                )

        super().save_model(request, obj, form, change)
